/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * newbixo.java
 *
 * Created on 30/Dez/2011, 15:34:28
 */

package vetclini;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.GregorianCalendar;
import javax.swing.JFrame;
import org.jdesktop.application.Action;
import veterinario.Animal;
import veterinario.Criacao;
import veterinario.Estimacao;

/**
 *
 * @author 2011101993 - Hugo Amaro
 * @author 2006130415 - Jorge Martins Alves
 */
public class newbixomain extends javax.swing.JPanel {

    private VetCliniView pai;
    private JFrame minhaframe;

    /** Creates new form newbixo */
    public newbixomain(JFrame fra) {
        initComponents();
        this.minhaframe = fra;
    }

    public void setPai(VetCliniView parent){
        this.pai = parent;
    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        cor = new javax.swing.JTextField();
        raca = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        concluir = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        tipo = new javax.swing.JComboBox();
        extraAtribute = new javax.swing.JTextField();
        extraAtributeLabel = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        dia = new javax.swing.JTextField();
        mes = new javax.swing.JTextField();
        ano = new javax.swing.JTextField();

        setName("Form"); // NOI18N

        cor.setName("cor"); // NOI18N

        raca.setName("raca"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(vetclini.VetCliniApp.class).getContext().getResourceMap(newbixomain.class);
        jLabel4.setText(resourceMap.getString("jLabel4.text")); // NOI18N
        jLabel4.setName("jLabel4"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(vetclini.VetCliniApp.class).getContext().getActionMap(newbixomain.class, this);
        concluir.setAction(actionMap.get("clickedConcluir")); // NOI18N
        concluir.setText(resourceMap.getString("butaoConcluir.text")); // NOI18N
        concluir.setName("butaoConcluir"); // NOI18N

        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N

        jLabel6.setText(resourceMap.getString("jLabel6.text")); // NOI18N
        jLabel6.setName("jLabel6"); // NOI18N

        tipo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Estimação", "Criação" }));
        tipo.setName("tipo"); // NOI18N
        tipo.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                tipoPropertyChange(evt);
            }
        });

        extraAtribute.setName("atributoExtra"); // NOI18N

        extraAtributeLabel.setText(resourceMap.getString("atributoExtraLabel.text")); // NOI18N
        extraAtributeLabel.setName("atributoExtraLabel"); // NOI18N

        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        dia.setText(resourceMap.getString("dia.text")); // NOI18N
        dia.setName("dia"); // NOI18N

        mes.setText(resourceMap.getString("mes.text")); // NOI18N
        mes.setName("mes"); // NOI18N

        ano.setText(resourceMap.getString("ano.text")); // NOI18N
        ano.setName("ano"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(26, 26, 26)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(extraAtributeLabel)
                            .addComponent(jLabel3)
                            .addComponent(jLabel2)
                            .addComponent(jLabel1)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabel6)
                                .addComponent(jLabel4)))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(tipo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(raca, javax.swing.GroupLayout.DEFAULT_SIZE, 142, Short.MAX_VALUE)
                            .addComponent(cor)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(dia, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(mes, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(ano, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(extraAtribute)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(116, 116, 116)
                        .addComponent(concluir)))
                .addContainerGap(32, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(10, 10, 10)
                .addComponent(jLabel6)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(tipo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(raca, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(cor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(dia, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(mes, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(ano, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(extraAtributeLabel)
                    .addComponent(extraAtribute, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(concluir)
                .addContainerGap(20, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void tipoPropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_tipoPropertyChange
        // TODO add your handling code here:
        if(tipo.getSelectedIndex()==0)
            extraAtributeLabel.setText("Nome:");
        else
            extraAtributeLabel.setText("Origem:");
    }

    @Action
    public void clickedConcluir() {
        boolean podeConcluir = true;
        String erro = "";
        int tipoAnimal = tipo.getSelectedIndex();
        if (raca.getText().length()==0){
            erro += "/ raça não preenchida";
            podeConcluir = false;
        }
        if (cor.getText().length()==0){
            erro += "/ cor não preenchida\r\n";
            podeConcluir = false;
        }
        if (extraAtribute.getText().length()==0){
            if(tipoAnimal==0){
                erro += "/ nome não preenchido\r\n";
            }else{
                erro += "/ origem não preenchida\r\n";
            }
            podeConcluir = false;
        }
        int a=0, m=0, d=0;
        try{
            d = Integer.parseInt(dia.getText());
            m = Integer.parseInt(mes.getText());
            a = Integer.parseInt(ano.getText());
        }
        catch (java.lang.NumberFormatException ne) {
            erro += "/ data inválida\r\n";
            podeConcluir = false;
            a=0; m=0; a=0;
        }
        SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
        if(d>0&&m>0&&m<=12&&a>0){
            
            Date testDate = null;
            try
	    {
	      testDate = dateFormat.parse(dia.getText()+"/"+m+"/"+ano.getText());
	    }
            catch (ParseException e)
	    {
	      erro += "/ data inválida (1)\r\n";
            
              podeConcluir = false;
	    }

            String data = "";
            if(d<10)
                data += 0;
            data += d+"/";
            if(m<10)
                data += 0;
            data += m+"/";
            data += a;
            if (!dateFormat.format(testDate).equals(data))
	    {
	      erro += "/ "+dateFormat.format(testDate).toString()+"data inválida (2)\r\n";
	      podeConcluir = false;
	    }
 
        }else {
            erro += "/ data inválida (3)\r\n";
	    podeConcluir = false;
        }
        GregorianCalendar nascimento = new GregorianCalendar(a,m-1,d);


        if(podeConcluir){
            if(tipoAnimal==0){
                Estimacao novoAnimal = new Estimacao(extraAtribute.getText(), raca.getText(), cor.getText(), nascimento);
                pai.addAnimalCliente(novoAnimal);
            }else{
                Criacao novoAnimal = new Criacao(extraAtribute.getText(), raca.getText(), cor.getText(), nascimento);
                pai.addAnimalCliente(novoAnimal);
            }
            
            pai.updateClientAnimalList();
            this.minhaframe.dispose();
        }else{
            janelaErro janelaErro = new janelaErro(erro);
            janelaErro.pack();
            janelaErro.setVisible(true);
            janelaErro.requestFocus();
        }
    }//GEN-LAST:event_tipoPropertyChange


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField ano;
    private javax.swing.JButton concluir;
    private javax.swing.JTextField cor;
    private javax.swing.JTextField dia;
    private javax.swing.JTextField extraAtribute;
    private javax.swing.JLabel extraAtributeLabel;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JTextField mes;
    private javax.swing.JTextField raca;
    private javax.swing.JComboBox tipo;
    // End of variables declaration//GEN-END:variables

}
